<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/11
 */

namespace App\Http\Requests;

use App\Services\DictionaryService;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;

/**
 * 广告模块表单验证
 * Class AdsRequest
 * @package App\Http\Requests
 */
class AdsRequest extends BaseRequest
{

    /**
     * 注入表单请求验证时自动校验
     *
     * @var bool
     */
    public $autoValidate = false;

    /**
     * 用户是否有权操作，true是，false否
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 字段验证规则
     *
     * @return array
     */
    public function rules()
    {
        $ads_space = $this->ads_space;
        $ads = $this->ad;
        return [
            'id' => 'required|integer',
            'created_user_id' => 'required|integer',
            'space_id' => 'required|integer',
            'name' => ['required', 'min:2', 'max:50'],
            'cover_image' => [
                function ($attribute, $value, $fail) use ($ads_space) {
                    if ($ads_space->type == 1 && $value == 0) {
                        $fail(__('ads.error_cover_image'));
                    }
                    AttachmentRequest::validity(__('ads.cover_image'), $value, $fail, 1);
                }
            ],
            'href' => 'nullable|max:100',
            'href_target' => 'nullable|max:100',
            'rel' => 'nullable|max:20',
            'partner' => 'nullable|max:50',
            'partner_linkman' => 'nullable|max:20',
            'partner_phone_number' => 'nullable|max:20',
            'description' => 'nullable|max:200',
            'cast_period' => [
                'required',
                'integer',
                Rule::in(DictionaryService::getItemsValuesArray('adsCastPeriodVar')),
            ],
            'start_time' => 'nullable|required_if:cast_period,2',
            'end_time' => 'nullable|required_if:cast_period,2',
            'sort' => 'required|integer',
            'status' => 'required|boolean',
            'sort.*.id' => 'required|integer',
            'sort.*.sort' => 'required|integer',
        ];
    }

    /**
     * 提示信息
     * @return array
     */
    public function messages()
    {
        return [
            'start_time.required_if' => __('ads.error_start_time'),
            'end_time.required_if' => __('ads.error_end_time'),
        ];
    }

    /**
     * 定义场景
     * @var \string[][]
     */
    public $scenes = [
        'store' => [
            'created_user_id',
            'space_id',
            'name',
            'cover_image',
            'href',
            'href_target',
            'rel',
            'partner',
            'partner_linkman',
            'partner_phone_number',
            'description',
            'cast_period',
            'start_time',
            'end_time',
            'status'
        ],
        'update' => [
            'name',
            'cover_image',
            'href',
            'href_target',
            'rel',
            'partner',
            'partner_linkman',
            'partner_phone_number',
            'description',
            'cast_period',
            'start_time',
            'end_time',
            'status'
        ],
        'destroy' => ['id'],
        'updateStatus' => ['status'],
        'updateSort' => ['sort.*.id', 'sort.*.sort']
    ];

    /**
     * 验证错误的自定义属性
     *
     * @return array
     */
    public function attributes()
    {
        return [
            'id' => __('common.id'),
            'name' => __('ads.name'),
            'space_id' => __('ads.space_id'),
            'cover_image' => __('ads.cover_image'),
            'href' => __('common.href'),
            'href_target' => __('common.href_target'),
            'rel' => __('common.rel'),
            'partner' => __('links.partner'),
            'partner_linkman' => __('common.linkman'),
            'partner_phone_number' => __('common.phone_number'),
            'description' => __('ads.description'),
            'cast_period' => __('ads.cast_period'),
            'start_time' => __('common.start_time'),
            'end_time' => __('common.end_time'),
            'sort' => __('common.sort'),
            'status' => __('common.status'),
            'sort.*.id' => __('common.id'),
            'sort.*.sort' => __('common.sort'),
        ];
    }

    /**
     * 验证前
     *
     * @return void
     */
    protected function prepareForValidation()
    {
        $this->offsetSet('space_id', $this->ads_space->id);
        $this->offsetSet('created_user_id', Auth::id());
        if (!isset($this->cover_image) || empty($this->cover_image)) $this->offsetSet('cover_image', 0);
        if (!isset($this->start_time) || empty($this->start_time)) $this->offsetSet('start_time', null);
        if (!isset($this->end_time) || empty($this->end_time)) $this->offsetSet('end_time', null);
    }
}
